package ypmits.sound {
	import flash.display.DisplayObject;	import flash.events.MouseEvent;	
	/**
	 * @author Hugo
	 * 
	 * The SoundWrapper enables you to easily add a mouse over, down, up and out
	 * sound to played for a DisplayObject.
	 * 
	 */
	public class SoundWrapper {
		
		private var _target : DisplayObject;
		private var _overSound : ISoundControl;
		private var _downSound : ISoundControl; 
		private var _upSound : ISoundControl;
		private var _outSound : ISoundControl;
		
		public static const FX_VOLUME : Number = 0.75;

		/**
		 * @param target The objects where the sounds are played back for on a MouseEvent
		 * @param overSoundDef The over sound Class definition
		 * @param downSoundDef The down sound Class definition
		 * @param upSoundDef The up sound Class definition
		 * @param outSoundDef The out sound Class defition
		 */
		public function SoundWrapper( target : DisplayObject, overSoundDef : ISoundControl = null,
									downSoundDef : ISoundControl = null, upSoundDef : ISoundControl = null, 
									outSoundDef : ISoundControl = null ) {
			
			_target = target;
			
			if(overSoundDef) {
				_overSound = overSoundDef;
				_overSound.volume = SoundWrapper.FX_VOLUME;
				_target.addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
			}
			if(downSoundDef) {
				_downSound = downSoundDef;
				_downSound.volume = SoundWrapper.FX_VOLUME;
				_target.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
			if(upSoundDef) {
				_upSound = upSoundDef;
				_upSound.volume = SoundWrapper.FX_VOLUME;
				_target.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			}
			if(outSoundDef) {
				_outSound = outSoundDef;
				_outSound.volume = SoundWrapper.FX_VOLUME;
				_target.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
			}
		}
		
		private function onMouseOver( e : MouseEvent ) : void {
			_overSound.play();
		}

		private function onMouseDown( e : MouseEvent ) : void {
			_downSound.play();
		}
		
		private function onMouseUp( e : MouseEvent ) : void {
			_upSound.play();
		}
		
		private function onMouseOut( e : MouseEvent ) : void {
			_outSound.play();
		}
	}
}
